// 入驻申请
<template>
   <div class="search">
     <Row>
        <Card>
          <Row class="operation">
            <Form inline :label-width="100" ref="searchForm" :model="searchForm" label-position="right">
              <Button @click="getDataList()" icon="md-refresh">刷新</Button>
              <Button @click="delAll" icon="md-trash">批量删除</Button>
              <Button @click="add" type="primary">添加</Button>
            </Form>
          </Row>

          <Row>
            <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort" @on-selection-change="changeSelect"></Table>
          </Row>

          <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
          </Row>
        </Card>
     </Row>

     <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="600" @on-cancel="cancelSubmit">
      <Form ref="form" :model="form" :label-width="100" :rules="formValidate" >
        <FormItem label="分佣类型" prop="profitTypeEnum">
          <Select v-model="form.profitTypeEnum" style="width:100%" @on-change="selectProfit">
            <Option value="FIRST">第一层分佣</Option>
            <Option value="TWO">第二成分佣</Option>
          </Select>
        </FormItem>
        <FormItem label="平台分成" prop="platformProfit">
          <Input v-model="form.platformProfit" style="width: 100%" placeholder="请输入平台分成" />
        </FormItem>
        <FormItem label="商家分成" prop="storeProfit" v-if="profitType === 'FIRST'">
          <Input v-model="form.storeProfit" style="width: 100%" placeholder="请输入商家分成" />
        </FormItem>
        <FormItem label="个人分成" prop="personProfit" v-if="profitType === 'TWO'">
          <Input v-model="form.personProfit" style="width: 100%" placeholder="请输入个人分成" />
        </FormItem>
        <FormItem label="推荐人分成" prop="recommendProfit" v-if="profitType === 'TWO'">
          <Input v-model="form.recommendProfit" style="width: 100%" placeholder="请输入推荐人分成" />
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="cancelSubmit">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
      </div>
    </Modal>
   </div> 
</template>

<script>
export default {
  data(){
    return{ 
      submitLoading: false,
      total:0,
      modalTitle: "",
      loading: true, // 表单加载状态
      data:[],//表单数据
      selectCount:0,
      selectList:[],
      searchForm:{
        pageSize:10,
        pageNumber:1
      },
      form: {
        id: "",
        profitTypeEnum: "",
        platformProfit: "",
        storeProfit: "",
        personProfit: "",
        recommendProfit: "",
      },
      modalVisible: false,
      formValidate: {
        profitTypeEnum: [{ required: true, message: "请选择分佣类型", trigger: "change" }],
        platformProfit: [{ required: true, message: "平台分成只能为正小数，请重新输入", trigger: "blur", pattern:/^[0]+.?[0-9]*$/ }],
        storeProfit: [{ required: true, message: "商家分成只能为正小数，请重新输入", trigger: "blur", pattern:/^[0]+.?[0-9]*$/ }],
        personProfit: [{ required: true, message: "个人分成只能为正小数，请重新输入", trigger: "blur", pattern:/^[0]+.?[0-9]*$/ }],
        recommendProfit: [{ required: true, message: "推荐人分成只能为正小数，请重新输入", trigger: "blur", pattern:/^[0]+.?[0-9]*$/ }],
      },
      columns: [
      // 表头
      {
        type: "selection",
        width: 60,
        align: "center"
      },
      {
        type: "index",
        width: 60,
        align: "center"
      },
      {
        title: "平台分成",
        key: "platformProfit",
        minWidth: 120,
        sortable: false,
      },
      {
        title: "商家分成",
        key: "storeProfit",
        minWidth: 120,
        sortable: false,
      },
      {
        title: "个人分成",
        key: "personProfit",
        minWidth: 120,
        sortable: false,
      },
      {
        title: "推荐人分成",
        key: "recommendProfit",
        minWidth: 120,
        sortable: false,
      },
      {
        title: "分佣类型",
        key: "profitTypeEnum",
        minWidth: 120,
        sortable: false,
      },
      {
        title: "创建时间",
        key: "createTime",
        minWidth: 120,
        sortable: false,
      },
      {
          title: "操作",
          width: 160,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params.row);
                    }
                  }
                },
                "编辑"
              ),
            //   h(
            //     "Button",
            //     {
            //       props: {
            //         type: "error",
            //         size: "small"
            //       },
            //       style: {
            //         marginRight: "5px",
            //       },
            //       on: {
            //         click: () => {
            //           this.remove(params.row);
            //         }
            //       }
            //     },
            //     "删除"
            //   ),
            ]);
          }
        }
      ],
      goodsDetail: {},
      modalType: 0,
      profitType: "",
      }
    },
    methods:{
      init(){
        this.getDataList()
      },
      cancelSubmit () {
        this.getDataList();
        this.modalVisible = false;
      },
      handleView(url) {
        this.imgUrl = url;
        this.visible = true;
      },
      getDataList(){
        this.loading = true;
        this.postRequest("/profitPercent/getProfitPercentList",this.searchForm).then(res =>{
          this.loading = false
          if(res.success){
            this.data = res.result.dataResult;
            this.total = res.result.totalNumber;
          } else {
            this.data = [];
            this.total = 0;
          }
        })
      },
      //搜索查询
      handleSearch(){
        this.getDataList()
      },
      changeSort(e) {
        this.searchForm.sort = e.key;
        this.searchForm.order = e.order;
        if (e.order === "normal") {
          this.searchForm.order = "";
        }
        this.getDataList();
      },
      changePage(v) {
        this.searchForm.pageNumber = v;
        this.getDataList();
      },
      changePageSize(v) {
        this.searchForm.pageSize = v;
        this.getDataList();
      },
      clearSelectAll(){
        this.$refs.searchForm.resetFields()
        this.getDataList()
      },
      handleSubmit () {
        this.$refs.form.validate(valid=> {
          if (valid) {
            if (this.form.profitTypeEnum === "FIRST") {
              this.form.personProfit = "0";
              this.form.recommendProfit = "0";
              if (Number(this.form.storeProfit) + Number(this.form.platformProfit) != 1) {
                this.$Message.warning("商家分成与平台分成相加必须为1！")
              } else {
                this.submit()
              }
            } else {
              this.form.storeProfit = "0";
              if (Number(this.form.personProfit) + Number(this.form.recommendProfit) + Number(this.form.platformProfit) != 1) {
                this.$Message.warning("个人分成、推荐人分成和平台分成相加必须为1！")
              } else {
                this.submit()
              }
            }
          }
        })
      },
      submit () {
        this.submitLoading = true;
        if (this.modalType === 0) {
          delete this.form.id;
          this.postRequest("/profitPercent/insertOrderUpdateProfitPercent",this.form).then(res=> {
            this.submitLoading = false
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
              this.modalVisible = false;
            } else {
              this.$Message.success(res.message);
              this.getDataList();
              this.modalVisible = false;
            }
          })
        } else {
          this.postRequest("/profitPercent/insertOrderUpdateProfitPercent",this.form).then(res=> {
            this.submitLoading = false
            if (res.success) {
              this.$Message.success("操作成功");
              this.getDataList();
              this.modalVisible = false;
            } else {
              this.$Message.success(res.message);
              this.getDataList();
              this.modalVisible = false;
            }
          })
        }
      },
      // 编辑
      edit (v) {
        console.log(v)
        this.modalType = 1;
        this.modalTitle = "编辑";
        this.form = v;
        if (v.profitTypeEnum === "第一层分佣") {
          this.form.profitTypeEnum = "FIRST";
          this.profitType = "FIRST"
        } else {
          this.form.profitTypeEnum = "TWO";
          this.profitType = "TWO"
        }
        delete this.form._index;
        delete this.form._rowKey
        this.modalVisible = true;
      },
      add () {
        this.modalTitle = "添加";
        this.modalType = 0;
        this.$refs.form.resetFields()
        this.modalVisible = true;
      },
      selectProfit (e) {
        console.log(e);
        this.profitType = e;
      },
      delAll (v) {
        this.$Modal.confirm({
            title: "删除",
            content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
            loading: true,
            onOk: () => {
            let ids = "";
            this.selectList.forEach(function(e) {
                ids += e.id + ",";
            });
            ids = ids.substring(0, ids.length - 1);
            var param = {
                ids: ids
            }
            this.postRequest("/profitPercent/removeProfitPercent",param).then(res=> {
                if (res.success) {
                    this.$Message.success("操作成功");
                    this.getDataList();
                    this.$Modal.remove()
                } else {
                    this.$Message.error(res.message);
                    this.getDataList()
                }
            })
            }
        });
      },
      changeSelect(e) {
        this.selectList = e;
        this.selectCount = e.length;
      },
    },
    mounted(){
      this.init()
    }
}
</script>

<style lang="less">
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-size: 13px;
        font-weight: 600;  
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        font-size: 13px;
        margin-left: 5px;
    }
}
</style>